/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.invocation.client.events;

import java.io.Serializable;

import org.nuxeo.gwt.habyt.invocation.client.Invocation;
import org.nuxeo.gwt.habyt.invocation.client.Notification;
import org.nuxeo.gwt.habyt.invocation.client.ObjectRef;

import com.google.gwt.event.shared.GwtEvent;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
@SuppressWarnings("serial")
public class NotificationEvent extends GwtEvent<NotificationEventHandler>
        implements Serializable {

    /**
     * Handler type.
     */
    private static Type<NotificationEventHandler> TYPE;

    public static Type<NotificationEventHandler> getType() {
        if (TYPE == null) {
            TYPE = new Type<NotificationEventHandler>();
        }
        return TYPE;
    }

    protected Notification notification;

    /**
     * Not null if notification is sent as the result of an invocation made by
     * the client.
     */
    protected Invocation invocation;

    public NotificationEvent(Notification notification) {
        this.notification = notification;
    }

    public NotificationEvent(Notification notification, Invocation invocation) {
        this.notification = notification;
        this.invocation = invocation;
    }

    public Invocation getInvocation() {
        return invocation;
    }

    public ObjectRef getInvocationTarget() {
        return invocation != null ? invocation.getTarget() : null;
    }

    public Notification getNotification() {
        return notification;
    }

    @Override
    public Type<NotificationEventHandler> getAssociatedType() {
        return getType();
    }

    @Override
    protected void dispatch(NotificationEventHandler handler) {
        handler.onNotification(this);
    }

}
